package com.travelingsalesmen.post;

import com.travelingsalesmen.post.rmi.IServer;
import com.travelingsalesmen.post.rmi.Server;
import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Store {

    private String storeName;
    private String address;
    private Catalog cat;
    private Manager manager;

    public Store() {
        storeName = "Travelling Bakers";
        address = "123 Bake St";
    }

    public void open() {
    }

    public Manager getManager() {
        if (manager == null) {
            manager = new Manager(this);
        }
        return manager;
    }

    public void setManager(Manager manager) {
        this.manager = manager;
    }

    public void startServer() {
        // Start the server in its own thread
        new Runnable() {

            public void run() {
                try {
                    IServer server = new Server();
                    Registry registry = LocateRegistry.createRegistry(1099);
                    registry.bind("server", server);
                } catch (AlreadyBoundException ex) {
                    Logger.getLogger(Store.class.getName()).log(Level.SEVERE, null, ex);
                } catch (AccessException ex) {
                    Logger.getLogger(Store.class.getName()).log(Level.SEVERE, null, ex);
                } catch (RemoteException ex) {
                    Logger.getLogger(Store.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }.run();
    }
}
